<?php defined('SYSPATH') OR die('No direct access allowed.');
/**
 * Contains examples of various Kohana library examples. You can access these
 * samples in your own installation of Kohana by going to ROOT_URL/examples.
 * This controller should NOT be used in production. It is for demonstration
 * purposes only!
 *
 * $Id: examples.php 4134 2009-03-28 04:37:54Z zombor $
 *
 * @package    Core
 * @author     Kohana Team
 * @copyright  (c) 2007-2008 Kohana Team
 * @license    http://kohanaphp.com/license.html
 */
 
 
class Products_Controller extends Web_Controller {
  
	const ALLOW_PRODUCTION = FALSE;
	public $template = 'kohana/template';
	var $all_count = 0;
 
 	function index(){
 	
		$this->template->content = new View('products');
		$this->template->content->error = false;

 		$this->id = ($this->uri->segment('pro'));
 		$this->fr_id = ($this->uri->segment('firm'));
 		$this->tp_id = ($this->uri->segment('type'));
		
		$this->template->content->items_ses = $items = $this->session->get('items');

		$offset = $this->uri->segment('page'); 
 
		$page_limit = 20;
		
		$base_url = 'products:'.$this->id;
		$table = 'bz_items';
		$where = 'bz_groups.gr_link = "'.$this->id.'" and bz_items.it_archive = 0';
		
		if(!empty($this->tp_id)){
			$where .= ' and bz_types.tp_link = "'.$this->tp_id.'"';
			$base_url .= '/type:'.$this->tp_id;
		}

		if(!empty($this->fr_id)){
			$where .= ' and bz_firms.fr_link = "'.$this->fr_id.'"';
			$base_url .= '/firm:'.$this->fr_id;
		}
		
		$this->template->content->items = $this->db->select($table.'.*, bz_types.tp_title, bz_firms.fr_title')->from($table)->
			join('bz_groups', 'bz_groups.gr_id',  $table.'.gr_id', 'left')->
			join('bz_types', 'bz_types.tp_id', $table.'.tp_id', 'left')->
			join('bz_firms', 'bz_firms.fr_id', $table.'.fr_id', 'left')->
				where($where)->offset($offset)->limit($page_limit)->orderby('it_model')->get();
 
		$this->template->content->firms = $this->db->select($table.'.*, bz_firms.fr_title, bz_firms.fr_link')->from($table)->
			join('bz_firms', 'bz_firms.fr_id', $table.'.fr_id', 'left')->
			join('bz_groups', 'bz_groups.gr_id',  $table.'.gr_id', 'left')->
			groupby('bz_firms.fr_id')->
				where('bz_groups.gr_link = "'.$this->id.'"')->where($table.'.it_archive = 0')->orderby('bz_firms.fr_title')->get();
				
		$this->template->content->types = $this->db->select('bz_types.tp_id, bz_types.tp_title, bz_types.tp_link')->from($table)->
			join('bz_types', 'bz_types.tp_id', $table.'.tp_id', 'left')->
			join('bz_groups', 'bz_groups.gr_id',  $table.'.gr_id', 'left')->
			groupby('bz_types.tp_id')->
				where('bz_groups.gr_link = "'.$this->id.'"')->where($table.'.it_archive = 0')->orderby('bz_types.tp_title')->get(); 
 

		$count_records = $this->db->select($table.'.it_id')->from($table)->
			join('bz_groups', 'bz_groups.gr_id',  $table.'.gr_id', 'left')->
			join('bz_types', 'bz_types.tp_id', $table.'.tp_id', 'left')->
			join('bz_firms', 'bz_firms.fr_id', $table.'.fr_id', 'left')->
				where($where)->count_records();
				
		if($count_records == 0){
			url::redirect();
		}
 
		foreach ($this->groups as  $key ){
			if(@$this->id == $key->gr_link) { 
				$this->crn = $key->gr_title; 
			}
		}
						
		$this->template->title = $this->crn;;
		$this->keywords = getKeyWords($this->crn);
		 
		$pagination = new Pagination(array(
			// Base_url will default to the current URI
			#'base_url'    => $base_url.'/page',
		
			// The URI segment (integer) in which the pagination number can be found
			// The URI segment (string) that precedes the pagination number (aka "label")
			'uri_segment'    => 'page',
		
			// You could also use the query string for pagination instead of the URI segments
			// Just set this to the $_GET key that contains the page number
			// 'query_string'   => 'page',
		
			// The total items to paginate through (probably need to use a database COUNT query here)
			'total_items'    => $count_records,
		
			// The amount of items you want to display per page
			'items_per_page' => $page_limit,
		
			// The pagination style: classic (default), digg, extended or punbb
			// Easily add your own styles to views/pagination and point to the view name here
			'style'          => 'digg',
		 
			// If there is only one page, completely hide all pagination elements
			// Pagination->render() will return an empty string
			'auto_hide'      => TRUE,
		));


		$this->template->content->pagination = $pagination->render('digg'); 

 
		$items_bin = $this->session->get('items');
		
		$this->template->content->empty_bin = true;
		
		if(!empty($items_bin)){

			$this->template->content->empty_bin = false;
			
			$where = null;
			foreach ($items_bin as $key => $count){
				$where .= $key.', ';
			}
			
			$this->template->content->bin_items = $this->db->select($table.'.*, bz_types.tp_title, bz_firms.fr_title')->from($table)->
				join('bz_types', 'bz_types.tp_id', $table.'.tp_id', 'left')->
				join('bz_firms', 'bz_firms.fr_id', $table.'.fr_id', 'left')->
					where('it_id in ('.substr($where, 0, -2).')')->
                                        where('it_archive = 0')->
                                            orderby('it_model')->get();
				
		}else{
			
		}
 
 	}
	
	function item(){
	
		$this->template->content = new View('item');
		$this->template->content->error = false;
		
 		$this->id = ($this->uri->segment('id'));
		$this->item = $this->db->select('*')->from('bz_items')->where('it_link = "'.$this->id.'"')->where('it_archive = 0')->
			join('bz_types', 'bz_types.tp_id', 'bz_items.tp_id', 'left')->
			join('bz_firms', 'bz_firms.fr_id', 'bz_items.fr_id', 'left')->
				get(); 
 			
		$this->template->content->item = $this->item = $this->item[0];
 #print_r($this->item);
		$this->template->title = $this->item->it_model.' - '.$this->item->fr_title;;
		
		$this->template->content->content = file_get_contents(SERVER_PATH.'desc/'.$this->item->it_note2); 
		$this->template->content->items_ses = $items = $this->session->get('items'); 
		 
		$this->keywords = getKeyWords($this->item->it_model.' '.$this->item->fr_title);
		
	}
}

?>